package com.maduar.mybatis.demo.service;

import com.maduar.mybatis.demo.service.abs.AbstractAppointment;
import com.maduar.mybatis.demo.service.abs.FirstAppointmentStrategy;
import com.maduar.mybatis.demo.service.abs.SecondAppointmentStrategy;
import com.maduar.mybatis.demo.util.ErrorUtils;
import com.maduar.mybatis.demo.vo.AppointmentCancelBaseVo;
import com.maduar.mybatis.demo.vo.FactoryAppointmentInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-04-17 13:34
 */
@Service
public class AppointFactoryService extends AbstractAppointment {

    @Autowired
    private AppointmentFactoryStrategy factoryStrategy;

    @Autowired
    private Environment environment;

    @SuppressWarnings("unchecked")
    private <T extends AbstractAppointment> T pickService(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }

        if (Objects.equals(environment.getProperty(FactoryAppointmentInterface.FIRST), type)) {
            return (T) factoryStrategy.getStrategy(type, FirstAppointmentStrategy.class);
        } else {
            return (T)factoryStrategy.getStrategy(type, SecondAppointmentStrategy.class);
        }
    }

    @Override
    public boolean cancel(AppointmentCancelBaseVo vo) {
        ErrorUtils.isNullRuntimeError(vo, "参数有误");
        String type = vo.getType();
        AbstractAppointment abstractAppointment = pickService(type);
        ErrorUtils.isNullRuntimeError(abstractAppointment, "参数有误");
        return abstractAppointment.cancel(vo);
    }

    @Override
    public String save() {
        return null;
    }

    @Override
    public String update() {
        return null;
    }

    @Override
    public String receivingReport() {
        return null;
    }
}
